import storage from '@/utils/storage'
import constant from '@/utils/constant'
import { login, logout, getInfo, smsLogin, mpLogin, xcxLogin, updateUserInfo } from '@/api/login/login'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { getMineBalance } from '@/api/sr/balance'
import { getMineCard } from '@/api/sr/card'

const user = {
  state: {
    token: getToken(),
    user: storage.get(constant.constant.user),
    roles: storage.get(constant.constant.roles),
    permissions: storage.get(constant.constant.permissions),
	balance: storage.get(constant.constant.balance),
	card: storage.get(constant.constant.card)
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_USER: (state, user) => {
      state.user = user
      storage.set(constant.constant.user, user)
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
      storage.set(constant.constant.roles, roles)
    },
    SET_PERMISSIONS: (state, permissions) => {
      state.permissions = permissions
      storage.set(constant.constant.permissions, permissions)
    },
	SET_BALANCE: (state, balance) => {
	  state.balance = balance
	  storage.set(constant.constant.balance, balance)
	},
	SET_CARD: (state, card) => {
	  state.card = card
	  storage.set(constant.constant.card, card)
	}	
  },

  actions: {
    // 登录
    Login({ commit }, userInfo) {
      const username = userInfo.phoneNumber.trim()
      const password = userInfo.password
      const code = userInfo.code
      const uuid = userInfo.uuid
      return new Promise((resolve, reject) => {
        login(username, password, code, uuid).then(res => {
          setToken(res.data.token)
          commit('SET_TOKEN', res.data.token)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    SmsLogin({ commit }, userInfo) {
      const phoneNumber = userInfo.phoneNumber.trim()
      const smsCode = userInfo.smsCode
      return new Promise((resolve, reject) => {
        smsLogin(phoneNumber, smsCode).then(res => {
          setToken(res.data.token)
          commit('SET_TOKEN', res.data.token)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
	MpLogin({ commit }, mpCode) {
	  return new Promise((resolve, reject) => {
	    mpLogin(mpCode).then(res => {
	      setToken(res.data.token)
	      commit('SET_TOKEN', res.data.token)
	      resolve()
	    }).catch(error => {
	      reject(error)
	    })
	  })
	},
    XcxLogin({ commit }, xcxCode) {
      return new Promise((resolve, reject) => {
        xcxLogin(xcxCode).then(res => {
          setToken(res.data.token)
          commit('SET_TOKEN', res.data.token)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
	// 获取用户信息
	UpdateUserInfo({ commit, state }, userInfo) {
	  return new Promise((resolve, reject) => {
	    updateUserInfo({
			encryptedData: userInfo.encryptedData,
			ivStr: userInfo.iv
		}).then(res => {
	      console.info("userInfo", JSON.stringify(userInfo.userInfo))
	      commit('SET_USER', JSON.stringify(userInfo.userInfo))
	      resolve(res)
	    }).catch(error => {
	      reject(error)
	    })
	  })
	},
    // 获取用户信息
    GetInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getInfo().then(res => {
          const user = res.data.user
          const avatarUrl = (user == null || user.avatarUrl == "" || user.avatarUrl == null) ? require("@/static/logo.jpg") : user.avatarUrl
          if (res.data.roles && res.data.roles.length > 0) {
            commit('SET_ROLES', res.data.roles)
            commit('SET_PERMISSIONS', res.data.permissions)
          } else {
            commit('SET_ROLES', ['ROLE_DEFAULT'])
          }
          commit('SET_USER', user)
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },
    //获取用户余额信息
    getMineBalance({ commit }) {
      return new Promise((resolve, reject) => {
        getMineBalance().then(res => {
		  console.info('getMineBalance', res)
		  commit('SET_BALANCE', res.data.balance)
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },
	//获取用户账户拥有的优惠卡
	getMineCard({ commit }) {
	  return new Promise((resolve, reject) => {
	    getMineCard().then(res => {
		  console.info('getMineCard', res.data)
		  commit('SET_CARD', JSON.stringify(res.data))
	      resolve(res)
	    }).catch(error => {
	      reject(error)
	    })
	  })
	},
    // 退出系统
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout(state.token).then(() => {
          commit('SET_TOKEN', '')
          commit('SET_USER', '')
          commit('SET_ROLES', [])
          commit('SET_PERMISSIONS', [])
          removeToken()
          storage.clean()
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    }
  }
}

export default user
